<template>
  <div class="container">
    <el-card>
      <!-- 搜索 -->
      <el-form ref="form" :model="pages" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input v-model="pages.keyword"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18" style="text-align: right">
            <el-button size="small" @click="clear">清除</el-button>
            <el-button size="small" type="primary" @click="onSearch"
              >搜索</el-button
            >
          </el-col>
        </el-row>
      </el-form>
      <el-alert
        :title="'数据一共' + count + '条'"
        type="info"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- form表单 -->
      <template>
        <el-table :data="data" style="width: 100%" v-loading="loading">
          <el-table-column prop="id" label="编号" width="280">
          </el-table-column>
          <el-table-column prop="questionType" label="题型" width="180">
            <template v-slot="{ row }">
              <p v-if="row.questionType === '1'">单选</p>
              <p v-else-if="row.questionType === '2'">多选</p>
              <p v-else>简答</p>
            </template>
          </el-table-column>
          <el-table-column prop="questionIDs" label="题目编号" width="280">
            <template v-slot="{ row }">
              <div v-for="(item, index) in row.questionIDs" :key="index">
                <a href="#" class="number" @click="showDialog(item.id)">
                  {{ item.number }}
                </a>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="addTime" label="录入时间" width="220">
          </el-table-column>
          <el-table-column prop="totalSeconds" label="答题时间" width="180">
          </el-table-column>
          <el-table-column prop="accuracyRate" label="正确率" width="180">
          </el-table-column>
          <el-table-column prop="userName" label="录入人" width="180">
          </el-table-column>
          <el-table-column label="操作" width="180">
            <template v-slot="{ row }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                plain
                circle
                @click="remove(row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页功能 -->
      <el-pagination
        style="text-align: right; margin-top: 20px; margin-bottom: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pages.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pages.pagesize"
        layout="prev, pager, next, sizes, jumper"
        background
        :total="count"
      >
      </el-pagination>
      <!-- 题目编号 组件-->
      <questions-preview
        :dialogVisible="dialogVisible"
        @close="dialogVisible = false"
        :questionItem="questionItem"
      />
    </el-card>
  </div>
</template>

<script>
import { randoms, removeRandoms, detail } from '@/api/hmmm/questions'
import questionsPreview from '../components/questions-preview.vue'

export default {
  components: { questionsPreview },
  data () {
    return {
      data: [],
      pages: {
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      count: 0,
      questionItem: {},
      dialogVisible: false,
      loading: false
    }
  },
  created () {
    this.getListData()
  },
  methods: {
    // 获取数据
    async getListData () {
      this.loading = true
      const { data } = await randoms(this.pages)
      // console.log(data)
      this.data = data.items
      this.count = data.counts
      this.loading = false
    },
    // 分页
    handleSizeChange (newSize) {
      this.pages.pagesize = newSize
      this.pages.page = 1
      this.pages.keyword = ''
      this.getListData()
      // this.clear()
    },
    // 页码
    handleCurrentChange (newPage) {
      this.pages.page = newPage
      this.pages.keyword = ''
      this.getListData()
      // this.clear()
    },
    // 搜索
    onSearch () {
      this.getListData()
      this.pages.page = 1
    },
    // 清空
    clear () {
      this.pages.keyword = ''
      this.pages.page = 1
      this.getListData()
    },
    // 删除
    async remove (id) {
      try {
        await this.$confirm('你确定要删除吗', '提示')
        await removeRandoms(id)
        this.$message.success('删除成功')
        this.getListData()
      } catch (e) {
        this.$message.error('已取消')
        return e
      }
    },
    // 题目编号
    async showDialog (id) {
      const { data } = await detail({ id })
      // console.log(data)
      this.questionItem = data
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped lang="less">
.number {
  color: #66b1ff;
}
</style>
